package util;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileHandler
{
	private StringBuffer stringBuffer;
	
	public FileHandler()
	{
		this.stringBuffer = new StringBuffer();
	}

	public String fileToString(String filepath,boolean crop)
	{
		try
		{
			FileReader reader = new FileReader(filepath);
			char[] buffer = new char[44];

			int length = reader.read(buffer);
			
			while (length != -1)
			{
				if(crop == false)
					stringBuffer.append(buffer, 0, length);
				else if(!(new String(buffer)).contains("<?xml"))
					stringBuffer.append(buffer, 0, length);

				length = reader.read(buffer);
			}
			
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
			
		return stringBuffer.toString();
	}
	
	public String fileToString(String filepath, StringBuffer stringBuffer, boolean crop)
	{
		this.stringBuffer = stringBuffer;
		
		return fileToString(filepath,crop);		
	}
	
	public StringBuffer getStringBuffer()
	{
		return stringBuffer;
	}

	public void setStringBuffer(StringBuffer stringBuffer)
	{
		this.stringBuffer = stringBuffer;
	}

}
